package com.study.行为型模式.命令模式;

public class RemoteController {

    //开按钮的命令数组
    private Command onCommand[];
    private Command offCommand[];

    private Command undoCommand;

    //构造器，完成对按钮初始化
    public RemoteController(){
        //默认有5组控制按钮
        onCommand = new Command[5];
        offCommand = new Command[5];

        //初始化这5组按钮
        for (int i = 0; i < 5; i++) {
            onCommand[i] = new NoCommand();
            offCommand[i] = new NoCommand();
        }
    }

    //给按钮设置需要的命令
    public void setCommand(int no,Command onCommand,Command offCommand){
        this.onCommand[no] = onCommand;
        this.offCommand[no] = offCommand;
    }

    //按下开按钮
    public void onButtonWasPushed(int no){
        //找到你按下的开按钮，并调用对应的方法
        onCommand[no].execute();
        //记录此次操作，用于撤销
        undoCommand = onCommand[no];
    }
    //按下关按钮
    public void offButtonWasPushed(int no){
        //找到你按下的开按钮，并调用对应的方法
        offCommand[no].execute();
        //记录此次操作，用于撤销
        undoCommand = offCommand[no];
    }
    //按下撤销按钮
    public void undoButtonWasPushed(){
        undoCommand.undo();
    }
}
